<?php

namespace app\index\controller;
session_start();
use think\Controller;
use think\Request;
use think\Db;

class Cinema extends Controller
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $data = Db::table('l_cinema')
                ->select();

        $arr = [];
        foreach($data as $v){
            $list = Db::table('l_sort')
                    ->alias('ls')
                    ->field('lf.filmname')
                    ->where('ls.cinema_id', $v['id'])
                    ->join('l_film lf', 'ls.film_id=lf.id')
                    ->select();
            $v['film'] = $list;
            $arr[] = $v;
        }

        return view('Cinema/index', [
            'list'=>$arr
        ]);
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function cinema($id)
    {
        $data = Db::table('l_cinema')
                ->where('id', $id)
                ->find();

        $list = Db::table('l_sort')
                ->alias('ls')
                ->field('ls.price, lf.filmname, lf.filmimg, lf.id')
                ->where('ls.cinema_id', $id)
                ->join('l_film lf', 'ls.film_id=lf.id')
                ->select();
        $data['film'] = $list;

        return view('Cinema/cinema', [
            'data'=>$data
        ]);
    }

    public function ajaxsort($id)
    {
        $data = Db::table('l_film')
                ->alias('lf')
                ->field('ls.*, lf.tongue, ld.studio_name, ld.studio_type')
                ->where('lf.id', $id)
                ->join('l_sort ls', 'lf.id=ls.film_id')
                ->join('l_studio ld', 'ls.studio_id=ld.id')
                ->select();

        return $data;
    }

    // 地址联动
    public function cha()
    {
        $rel = Request()->get();
        $data = Db::table('lamp_address')->whereLike('name', '%市')->select();
        if(!empty($rel['upid'])){
            $data = Db::table('lamp_address')->where('upid', $rel['upid'])->select();
        }

        if(!$data){
            $data = false;
        }

        echo json_encode($data);
    }

    // 分类
    public function classification(Request $request)
    {
        $rel = $request->post();
        $name = Db::table('lamp_address')
                ->field('name')
                ->where('id', $rel['id'])
                ->find();
        
        $data = Db::table('l_cinema')
                ->where('town', $name['name'])
                ->select();

        $arr = [];
        foreach($data as $v){
            $list = Db::table('l_sort')
                    ->alias('ls')
                    ->field('lf.filmname')
                    ->where('ls.cinema_id', $v['id'])
                    ->join('l_film lf', 'ls.film_id=lf.id')
                    ->select();
            $v['film'] = $list;
            $arr[] = $v;
        }

        return $arr;
    }

}
